package com.macro.mall.portal.controller.commission;

import com.github.pagehelper.Page;
import com.macro.mall.commission.domain.OrderSettle;
import com.macro.mall.commission.entity.OrderSettleEntity;
import com.macro.mall.commission.service.OrderSettleService;
import com.macro.mall.commission.service.bo.PagedQueryOrderSettleInput;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.portal.dto.commission.PagedQueryOrderSettleReqDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 订单佣金结算控制器
 *
 * @author xuyanjun
 * @date 2019-12-16
 */
@Api(tags = "OrderSettleController", description = "订单佣金结算")
@Slf4j(topic = "OrderSettleController")
@RestController
@RequestMapping("/orderSettle")
public class OrderSettleController {

    @Autowired
    private OrderSettleService orderSettleService;

    /**
     * 分页查询订单佣金结算
     *
     * @return 分页后的订单佣金结算
     */
    @ApiOperation("分页查询订单佣金结算")
    @PostMapping("pagedQuery")
    public CommonPage<OrderSettle> pagedQuery(@RequestBody PagedQueryOrderSettleReqDto reqDto) {
        PagedQueryOrderSettleInput pagedQueryOrderSettleInput = new PagedQueryOrderSettleInput();
        pagedQueryOrderSettleInput
                .setPageIndex(reqDto.getPageIndex())
                .setPageSize(reqDto.getPageSize());

        Page<OrderSettleEntity> page = orderSettleService.pagedQuery(pagedQueryOrderSettleInput);

        List<OrderSettle> orderSettles = orderSettleService.entityToDomain(page);

        return CommonPage.restPage(orderSettles, page);
    }

    /**
     * 根据id获取订单佣金结算
     *
     * @param id id
     * @return 订单佣金结算
     */
    @ApiOperation("根据id获取订单佣金结算")
    @GetMapping("{id}")
    public CommonResult<OrderSettle> getById(@PathVariable long id) {
        if (id <= 0) {
            throw new BusinessException("要获取的订单佣金结算Id不能为空。");
        }

        OrderSettleEntity orderSettleEntity = orderSettleService.getById(id);
        if (orderSettleEntity == null) {
            throw new BusinessException(String.format("要获取的订单佣金结算id:%s不存在。", id));
        }

        OrderSettle orderSettle = orderSettleService.entityToDomain(orderSettleEntity);
        return CommonResult.success(orderSettle);
    }

    /**
     * 根据批量id获取订单佣金结算
     *
     * @param ids ids
     * @return 订单佣金结算
     */
    @ApiOperation("根据批量id获取订单佣金结算")
    @PostMapping("listByBatchIds")
    public CommonResult<List<OrderSettle>> listByBatchIds(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("要获取的订单佣金结算Id集合不能为空。");
        }

        List<OrderSettleEntity> orderSettleEntities = orderSettleService.listByBatchIds(ids);

        List<OrderSettle> orderSettles = orderSettleService.entityToDomain(orderSettleEntities);
        return CommonResult.success(orderSettles);
    }

}

